<?php
namespace Home\Controller;
use Think\Controller;

class CommentController extends Controller{
	//实例化订单详情表
	public function index(){

		$uid = $_SESSION['id'];
		$detail=M('detail');
		$arr['user_id']=$_SESSION['id'];
		$arr['order_number']=$_GET['order_number'];
		if($_GET['goodsid']){
			$arr['goods_id']=$_GET['goodsid'];
		}


		$details=$detail->where($arr)
		                ->select();

		$comm = M('comment');
		foreach($details as $v){
			$comment["$v[goods_id]"] = $comm->where(array('goods_id'=>$v['goods_id'],'user_id'=>$uid))->find();
		}


		$this->assign('detail',$details);
		$this->assign('comment',$comment);
		$this->display();
	}
	//添加评价
	public function add(){

		if ($_FILES['commentic']['name']){
			$upload = new \Think\Upload();
			// 实例化上传类
			$upload->rootPath  = './Public/Upload/';
			$upload->savePath  = '';
			$upload->maxSize   =   2*1024*1024 ;
			$upload->exts = array('jpg', 'gif', 'png', 'jpeg');
			$upload->saveName =time().'_'.mt_rand();
			// 设置附件上传大小
			$result=$upload->upload();

			if(!$result){
				$this->error('上传图片失败，失败信息为:'.$upload->getError(),U('Comment/index'));
			}else {
				//接受上传信息
				$_POST['commentic'] = $result['commentic']['savepath'] . $result['commentic']['savename'];
			}
		}


		//插入数据库（商品所有信息）
		$_POST['commenttime']=date('Y-m-d H:i:s',time());
		$_POST['user_id']=$_SESSION['id'];
		$_POST['user_name']=$_SESSION['username'];
		$order_number=$_POST['order_number'];
		// 实例化评价表
		$comment=M('comment');
		$comment->create();
		if($comment->add($_POST)){
			$this->success('评价提交成功',''.__MODULE__.'/Comment/index/order_number/'.$order_number.'');
		}else{
			$this->error('评价失败');
		}
	}

}
